package com.zysc.processflow.service.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zysc.processflow.common.util.StringUtils;
import com.zysc.processflow.entity.*;
import com.zysc.processflow.mapper.HandleprocesslineMapper;
import com.zysc.processflow.mapper.HandleprocessMapper;
import com.zysc.processflow.pojo.ProcessConfig;
import com.zysc.processflow.service.IHandleprocesslineService;
import com.zysc.processflow.common.base.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 处置流程节点连线表 服务实现类
 * </p>
 *
 * @author 正元智慧城市_
 * @since 2023-07-21
 */
@Service
public class HandleprocesslineServiceImpl extends BaseService<HandleprocesslineMapper, Handleprocessline> implements IHandleprocesslineService {
    @Autowired
    HandleprocessMapper handleprocessMapper;
    /**
     * 根据流程ID获取流程线配置信息
     * @param processId 流程ID
     * @return 流程线配置信息列表
     */
    @Override
    public List<ProcessConfig.LineConfig> getLinesConfigByProcessId(String processId) {
        List<ProcessConfig.LineConfig> result = new ArrayList<>();
        if (!StringUtils.isNull(processId)) {
            LambdaQueryWrapper<Handleprocessline> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Handleprocessline::getProcessid, processId);
            List<Handleprocessline> list = this.list(queryWrapper);
            Handleprocess handleprocess = handleprocessMapper.selectById(processId);
            for (Handleprocessline line : list) {
                // 获取旧配置信息
                String oldConfig = handleprocess.getConfig();
                // 创建流程线配置对象
                ProcessConfig.LineConfig lineConfig = new ProcessConfig.LineConfig();
                lineConfig.setFrom(line.getSourcenodeid()); // 设置起始节点ID
                lineConfig.setTo(line.getTargetnodeid()); // 设置目标节点ID
                lineConfig.setLabel(line.getCondition()); // 设置线的标签
                lineConfig.setConditiontype(line.getConditiontype()); // 设置线的条件类型
                lineConfig.setConditionvalue(line.getConditionvalue());
                try{
                    // 获取线列表
                    JSONArray lineList = JSON.parseObject(oldConfig).getJSONArray("lineList");
                    lineList.stream().forEach(item -> {
                        Map mapitem = (Map) item;
                        // 判断起始节点和目标节点是否匹配
                        if (mapitem.get("from").equals(line.getSourcenodeid()) && mapitem.get("to").equals(line.getTargetnodeid())) {
                            lineConfig.setConnector((String) mapitem.get("connector")); // 设置连接器
                            lineConfig.setAnchors((String)mapitem.get("anchors")); // 设置锚点
                            lineConfig.setPaintStyle((String)mapitem.get("paintStyle")); // 设置绘制样式
                        }
                    });
                }catch(Exception e){}
                result.add(lineConfig); // 添加流程线配置对象到结果列表
            }
        }
        return result; // 返回流程线配置信息列表
    }

}
